home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / mac_file / vendor_d / neuralwa / nw2v50 / stockio.c < prev    next >
Text File  |  1993-08-23  |  20KB  |  754 lines

  1. /* stock.c */
  2. /************************************************************************
  3.  * Copyright(C) 1987-1992 NeuralWare Inc                                *
  4.  * Penn Center West, IV-227, Pittsburgh, PA 15276                       *
  5.  * Telephone: (412) 787-8222    FAX: (412) 787-8220                     *
  6.  *                                                                      *
  7.  * All rights reserved.  No part of this program may be reproduced,     *
  8.  * stored in a retrieval system, or transmitted, in any form or by any  *
  9.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  10.  * without the prior written permission of the copyright owner,         *
  11.  * NeuralWare, Inc.                                                     *
  12.  *                                                                      *
  13.  *                          PROPRIETARY NOTICE                          *
  14.  *                                                                      *
  15.  * This document is the property of NeuralWare, Inc. and contains       *
  16.  * trade-secrets and other proprietary information.  The information    *
  17.  * herein is reserved as proprietary to NeuralWare, and is not to be    *
  18.  * published, reproduced, copied, disclosed, used, or reverse           *
  19.  * engineered without the express written consent of a duly authorized  *
  20.  * representative of NeuralWare.                                        *
  21.  ************************************************************************
  22.  */
  23.  
  24. #define UIO_SERVER             1
  25. #define SERVER_EMULATOR_FILE   1
  26. #define SKIP_COMPAT_DATA       1
  27. #include "userutl.h"
  28.  
  29. #include <math.h>
  30.  
  31. #include "uio_strc.h"
  32. #include "uio_tran.h"
  33. #include "uio_reqs.pro"
  34. #include "uio_cmds.pro"
  35.  
  36. #ifdef MAC
  37. #include "macuio.redef"
  38. #endif
  39.  
  40. /*   */
  41. /************************************************************************
  42.  *                                                                      *
  43.  *  Window management                                                   *
  44.  *                                                                      *
  45.  ************************************************************************
  46.  */
  47.  
  48. static NINT *null_ptr  = 0;
  49.  
  50. #define W0  1   /* main window */
  51. #define CHT 16    /* character height */
  52.  
  53. int W0_Wid  = {480};  /* basic defining parameters */
  54. int W0_Ht   = {200};  /* height of graph */
  55. int W0_Sep  = { 50};  /* separation */
  56. int W0_Pix  = {  1};  /* pixel size */
  57.  
  58. #define MX_ALL  (10)        /* x-for all messages */
  59. #define MY_1  (MX_ALL+10+(W0_Sep*1)/2)  /* y-for messages */
  60. #define MY_2  (MX_ALL+10+(W0_Sep*3)/2)
  61. #define MY_3  (MX_ALL+10+(W0_Sep*5)/2)
  62.  
  63. #define EX0 MX_ALL
  64. #define EX1 (W0_Wid-MX_ALL)
  65. #define EY0 (MX_ALL)
  66. #define EY1 (W0_Ht-MX_ALL)
  67. #define EHT (W0_Ht-2*MX_ALL)
  68.  
  69. /*   */
  70. /************************************************************************
  71.  *                                                                      *
  72.  *  SetParms() - set up basic display size parameters                   *
  73.  *                                                                      *
  74.  ************************************************************************
  75.  */
  76.  
  77. void SetParms()
  78. {
  79.    NINT  xsize, ysize, ncolor, chrx, chry;   /* graphic parms */
  80.    NINT  ifx, ify;                           /* integer factor */
  81.    
  82.    ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry );
  83.    
  84.    ifx = (REAL)xsize / (REAL)W0_Wid;
  85.    ify = (REAL)ysize / (REAL)W0_Ht;
  86.    
  87.    if ( ifx > 1 ) {
  88.       W0_Wid *= ifx;
  89.    }
  90.    if ( ify > 1 ) {
  91.       W0_Ht  *= ify;
  92.       W0_Sep *= ify;
  93.    }
  94.    if ( ifx > 1 && ify > 1 ) {
  95.       W0_Pix = 2;
  96.    }
  97.    
  98.    return;
  99. }
  100. /*   */
  101. /************************************************************************
  102.  *                                                                      *
  103.  *  PlotArray() - plot a window full of data                            *
  104.  *                                                                      *
  105.  ************************************************************************
  106.  */
  107.  
  108. #ifdef MAC
  109. #define MAIN_COLOR  2
  110. #define AUX_COLOR   5
  111. #else
  112. #define MAIN_COLOR  6
  113. #define AUX_COLOR  13
  114. #endif
  115.  
  116. typedef struct _WinBlk {
  117.    NINT    Win;           /* window handle */
  118.    TEXT   *Title;         /* title */
  119.    NINT    X0,Y0;         /* coordinates of origin */
  120.    NINT    X1,Y1;         /* coordinates of top right */
  121.    NINT    XOff;          /* x-offset */
  122.    NINT    XInc;          /* x-increment */
  123.    REAL    Scale, Offset; /* scaling */
  124.    NINT    px, py;        /* previous x,y */
  125.    NINT    ArraySize;     /* array size */
  126.    NINT    cwx;           /* current work index */
  127.    SREAL  *SavArray;      /* save array */
  128.    TEXT   *SavFlag;       /* save flags */
  129. } WB;
  130.  
  131.  
  132. VOID PlotSet   ARGLIST ((WB *, NINT, TEXT *,
  133.                          NINT, NINT, NINT, NINT, NINT, NINT,
  134.                          REAL, REAL, SREAL *, TEXT *, NINT ));
  135. VOID PlotClear ARGLIST (( WB * ));
  136. VOID PlotArray ARGLIST (( WB *, SREAL *, SREAL *, NINT, NINT ));
  137. VOID PlotSave  ARGLIST (( WB * ));
  138. VOID PlotNext  ARGLIST (( WB *, REAL, REAL, NINT, NINT ));
  139.  
  140.  
  141. VOID PlotSet( WBp, Win, Title,
  142.              X0,Y0, X1,Y1, XOff, XInc,
  143.              Scale, Offset, SaveA, SaveF, NSave )
  144. WB      *WBp;     /* window block pointer */
  145. NINT     Win;
  146. TEXT    *Title;
  147. NINT     X0,Y0, X1,Y1;
  148. NINT     XOff;
  149. NINT     XInc;
  150. REAL     Scale, Offset;
  151. SREAL   *SaveA;
  152. TEXT    *SaveF;
  153. NINT     NSave;
  154. {
  155.    WBp->Win    = Win;
  156.    WBp->Title  = Title;
  157.    WBp->X0     = X0;
  158.    WBp->Y0     = Y0;
  159.    WBp->X1     = X1;
  160.    WBp->Y1     = Y1;
  161.    WBp->XOff   = XOff;
  162.    WBp->XInc   = XInc;
  163.    WBp->Scale  = Scale;
  164.    WBp->Offset = Offset;
  165.    WBp->px     = X0 - XInc + (XOff * XInc);
  166.    WBp->py     = Y0;
  167.    WBp->cwx    = 0;
  168.    WBp->ArraySize = NSave;
  169.    WBp->SavArray  = SaveA;
  170.    WBp->SavFlag   = SaveF;
  171. }
  172.  
  173.  
  174. VOID PlotClear( WBp )
  175. WB  *WBp;
  176. {
  177.    NINT wx;
  178.    
  179.    WBp->px = WBp->X0 - WBp->XInc + (WBp->XOff * WBp->XInc);
  180.    WBp->py = WBp->Y0;
  181.    WBp->cwx = 0;
  182.    
  183.    if ( WBp->SavArray != (SREAL *)null_ptr ) {
  184.       for( wx = 0; wx < WBp->ArraySize; wx++ ) {
  185.          WBp->SavArray[wx] = 0.0;
  186.          WBp->SavFlag[wx]  = 0;
  187.       }
  188.    }
  189.    
  190.    /* clear out the background */
  191.    ug_boxf( WBp->Win, 0, 0, WBp->X0, WBp->Y0, WBp->X1, WBp->Y1 );
  192.    
  193.    /* outline box */
  194.    ug_box( WBp->Win,  8, 0, WBp->X0-1, WBp->Y0-1, WBp->X1+1, WBp->Y1+1, 0 );
  195.    
  196.    return;
  197. }
  198.  
  199.  
  200.  
  201. VOID PlotArray( WBp, MDp, ADp, NPts, off )
  202. WB     *WBp;   /* window block pointer */
  203. SREAL  *MDp;   /* main data array */
  204. SREAL  *ADp;   /* aux data array */
  205. NINT    NPts;  /* # of points in the array */
  206. NINT    off;   /* offset for multiple plots */
  207. {
  208.    NINT cx, cy;   /* current x,y point */
  209.    NINT px, py;   /* previous x,y point */
  210.    NINT ay;       /* y-coordinate of alternate data */
  211.    NINT nx;       /* index */
  212.    NINT lcolor;   /* line color */
  213.    
  214.    if ( off == 0 ) {
  215.       /* clear out the background */
  216.       ug_boxf( WBp->Win, 0, 0, WBp->X0, WBp->Y0, WBp->X1, WBp->Y1 );
  217.       
  218.       /* outline box */
  219.       ug_box( WBp->Win,  8, 0, WBp->X0-1,WBp->Y0-1,WBp->X1+1,WBp->Y1+1, 0 );
  220.    }
  221.    
  222.    /* plot the data */
  223.    px = WBp->X0 - WBp->XInc + WBp->XOff * WBp->XInc;
  224.    py = px;
  225.    lcolor = ADp == (SREAL *)null_ptr?MAIN_COLOR:AUX_COLOR;
  226.    for( nx = 0; nx < NPts && px < WBp->X1; nx++ ) {
  227.       /* next x-location */
  228.       cx = px + WBp->XInc;
  229.       
  230.       /* y-coordinate & clip it */
  231.       cy = off+ WBp->Y0 + ((MDp[nx] + WBp->Offset) * WBp->Scale);
  232.       if ( cy < WBp->Y0 ) cy = WBp->Y0;
  233.       if ( cy > WBp->Y1 ) cy = WBp->Y1;
  234.       if ( cx > WBp->X1 ) cx = WBp->X1;
  235.       if ( nx != 0 ) {
  236.          ug_line( WBp->Win, lcolor, 0, px, py, cx, cy, 0 );
  237.       }
  238.       
  239.       /* auxiliary data */
  240.       if ( ADp != (SREAL *)null_ptr ) {
  241.          ay = off+ WBp->Y0 + ((ADp[nx] + WBp->Offset) * WBp->Scale);
  242.          if ( ay < WBp->Y0 )  ay = WBp->Y0;
  243.          if ( ay > WBp->Y1 )  ay = WBp->Y1;
  244.          if ( W0_Pix > 1 )
  245.             ug_boxf(WBp->Win,  MAIN_COLOR, 0,
  246.                     cx-(W0_Pix-1), ay-(W0_Pix-1),
  247.                     cx+(W0_Pix-1), ay+(W0_Pix-1) );
  248.          else
  249.             ug_point( WBp->Win,  MAIN_COLOR, 0, cx, ay );
  250.       }
  251.       /* track the last point */
  252.       px = cx;
  253.       py = cy;
  254.    }
  255.    
  256.    if ( WBp->Title != (char *)null_ptr )
  257.       ug_puts( WBp->Win, 15, 0, WBp->X0+5, WBp->Y1-15, WBp->Title, 0 );
  258.    
  259.    return;
  260. }
  261.  
  262. VOID PlotSave( WBp )
  263. WB  *WBp;
  264. {
  265.    NINT n;
  266.    
  267.    n = WBp->cwx;
  268.    if ( WBp->SavFlag[WBp->ArraySize-1] != 0 )
  269.       n = WBp->ArraySize;
  270.    
  271.    if ( WBp->Scale > 0.0 )
  272.       PlotArray( WBp, WBp->SavArray, (SREAL *)null_ptr, n, 0 );
  273. }
  274.  
  275. VOID PlotNext( WBp, MV, AV, af, cf )    /* plot next point */
  276. WB    *WBp;   /* window block pointer */
  277. REAL   MV;    /* main value */
  278. REAL   AV;    /* auxiliary value */
  279. NINT   af;    /* aux data flag */
  280. NINT   cf;    /* cursor flag */
  281. {
  282.    NINT ay;
  283.    NINT cx, cy;
  284.    
  285.    if ( cf != 0 && WBp->Scale > 0.0 ) {
  286.       if ( WBp->X0 <= WBp->px && WBp->px < WBp->X1 ) {
  287.          ug_line( WBp->Win, 0, 0, WBp->px+1,WBp->Y0,WBp->px+1,WBp->Y1, 0 );
  288.       }
  289.    }
  290.    
  291.    cx = WBp->px + WBp->XInc;
  292.    cy = WBp->Y0 + (MV + WBp->Offset) * WBp->Scale;
  293.    if ( cy < WBp->Y0 )  cy = WBp->Y0;
  294.    if ( cy > WBp->Y1 )  cy = WBp->Y1;
  295.    if ( cx > WBp->X1 )  cx = WBp->X1;
  296.    if ( WBp->px < cx && WBp->Scale > 0.0 &&
  297.        WBp->px >= (WBp->X0 + WBp->XOff * WBp->XInc) ) {
  298.       ug_line( WBp->Win, MAIN_COLOR, 0, WBp->px, WBp->py, cx, cy, 0 );
  299.       if ( af ) {
  300.          ay = WBp->Y0 + (AV + WBp->Offset) * WBp->Scale;
  301.          if ( ay < WBp->Y0 ) ay = WBp->Y0;
  302.          if ( ay > WBp->Y1 ) ay = WBp->Y1;
  303.          ug_point( WBp->Win, MAIN_COLOR, 0, cx, ay );
  304.       }
  305.    }
  306.    if ( cx >= WBp->X1 ) cx = WBp->X0;    /* wrap */
  307.    if ( WBp->SavArray != (SREAL *)null_ptr ) {
  308.       WBp->SavArray[WBp->cwx] = MV;
  309.       WBp->SavFlag[WBp->cwx]  = 1;
  310.       WBp->cwx++;
  311.       if ( WBp->cwx >= WBp->ArraySize || cx == WBp->X0 ) {
  312.          WBp->cwx = 0;     /* force wrap */
  313.          cx = WBp->X0;
  314.       }
  315.    }
  316.    
  317.    WBp->px = cx;
  318.    WBp->py = cy;
  319.    
  320.    if ( cf != 0 && WBp->Scale > 0.0 && WBp->px < WBp->X1 ) {
  321.       ug_line( WBp->Win, 15, 0, WBp->px+1, WBp->Y0, WBp->px+1, WBp->Y1, 0 );
  322.    }
  323.    
  324.    return;
  325. }
  326. /*   */
  327. /************************************************************************
  328.  *                                                                      *
  329.  *  Common data & statics                                               *
  330.  *                                                                      *
  331.  ************************************************************************
  332.  */
  333.  
  334. /*
  335.   There are three windows:
  336.   top - 4-week moving average w/actual data too
  337.   mid - neural network
  338.   bot - original data
  339.   */
  340.  
  341. WB  MWB = {0};    /* main */
  342.  
  343. /* data set definitions */
  344.  
  345. #define NTPTS     491               /* number of pts in data file */
  346. #define SetSize   450               /* size of training set */
  347. SREAL      TrainSet[NTPTS+1] = {0.0};  /* current training set */
  348. SREAL      NetPred[NTPTS+1]  = {0.0};  /* network prediction */
  349. SREAL      TenPred[NTPTS+1]  = {0.0};  /* moving average prediction */
  350. NINT       InputLen   = 0;        /* input length */
  351. NINT       TSetL      = 0;        /* training set length */
  352. NINT       DataCtr    = 0;        /* data counter */
  353. SREAL      LastDR     = 0.0;      /* last desired result */
  354. NINT       xlag       = 14;       /* first x-coordinate for network out */
  355. NINT       HitRate    = 0;        /* # of successful times it predicts trend*/
  356. NINT       HitTen     = 0;        /* 10-week hit rate */
  357. NINT       TotalCt    = 0;        /* Total # of recalls */
  358. SREAL      PrevWk     = 0.0;      /* previous week output */
  359. SREAL      PrevPrd    = 0.0;      /* previously predicted output */
  360. SREAL      CurTen     = 0.0;      /* current 10-wk moving average */
  361. SREAL      PrevTen    = 0.0;      /* previous 10-wk moving average */
  362. REAL       maxe       = 0.0;      /* max error (%) */
  363. NINT       maxect     = 0;        /* max error count */
  364. REAL       maxe2      = 0.0;      /* max error > 20% */
  365. NINT       maxe2ct    = 0;        /* max error 2 count */
  366. REAL       TotErr     = 0.0;      /* total error over all cases */
  367.  
  368. /*   */
  369. /************************************************************************
  370.  *                                                                      *
  371.  *  Training Set Input / Check it is there Routines                     *
  372.  *                                                                      *
  373.  ************************************************************************
  374.  */
  375.  
  376. NINT IsTSOk()
  377. {
  378.    if ( TSetL <= 0 )
  379.       return(UIO_ERROR);
  380.    else
  381.       return(UIO_OK);
  382. }
  383.  
  384.  
  385. NINT ReadTS( buf )
  386. TEXT    *buf;   /* work buffer */
  387. {
  388.    FILE   *fp;    /* input file pointer */
  389.    TEXT   *sp;    /* input string */
  390.    NINT    c;     /* work character */
  391.    NINT    wx;    /* work index */
  392.    
  393.    TSetL = 0;
  394.    for(;;) {
  395.       PutStr ("What is the training input File (<RETURN> for default) ?");
  396.       sp = GetStr();
  397.       while( *sp && *sp <= ' ' ) sp++;  /* skip leading white space */
  398.       if ( *sp == '\0' ) {
  399.          sp = "sp500.in";      /* default string */
  400.       }
  401.       
  402.       fp = fopen (sp, "r");    
  403.       if (fp == (FILE *) 0) {
  404.          sprintf( buf, "Could not open <%s>\n", sp );
  405.          PutStr( buf );
  406.          continue;
  407.       }
  408.       
  409.       for ( wx = 0; wx < NTPTS;)   {
  410.          if ( fgets( &buf[0], 80, fp ) == 0 ) break;
  411.          
  412.          /* kill any comments */
  413.          
  414.          for( sp = &buf[0]; (c = *sp) != '\0'; sp++ ) {
  415.             if ( c == '!' || c == '*' || c == '\r' || c == '\n' )
  416.                break;
  417.          }
  418.          *sp = '\0';     /* kill comments, etc */
  419.          
  420.          /* skip leading space and check for blank lines */
  421.          
  422.          for( sp = &buf[0]; *sp && *sp <= ' '; ) sp++;
  423.          if ( *sp == '\0' ) continue;
  424.          
  425.          sscanf( buf, "%f", &TrainSet[wx] );
  426.          wx++;
  427.       }
  428.       TSetL = wx;
  429.       fclose(fp);   
  430.       
  431.       if ( TSetL == 0 ) {
  432.          PutStr( "Nothing in data set, try again\n" );
  433.          continue;
  434.       }
  435.       
  436.       break;
  437.    }
  438.    
  439.    return( 0 );
  440. }
  441.  
  442. /*******************************************************************/
  443. /*                                                                 */
  444. /* Functions necessary for handling the User I/O package itself.   */
  445. /*                                                                 */
  446. /*******************************************************************/
  447.  
  448. TEXT         buf[100], *sp;
  449.  
  450. NINT UIO_Init(file)
  451. TEXT *file;
  452. {
  453.    NINT ret_val = UIO_OK;
  454.    
  455.    return(ret_val);
  456. }
  457.  
  458.  
  459. NINT UIO_Term(process)
  460. NINT process;
  461. {
  462.    NINT ret_val = UIO_OK;
  463.    
  464.    return(ret_val);
  465. }
  466.  
  467. NINT UIO_Attention()
  468. {
  469.    NINT ret_val = UIO_OK;
  470.    
  471.    
  472.    return(ret_val);
  473. }
  474.  
  475. NINT UIO_Learn_Start()
  476. {
  477.    if ( TSetL <= 0 )
  478.       ReadTS( &buf[0] ); /* read data */
  479.  
  480.    return ( IsTSOk() );
  481. }
  482.  
  483. NINT UIO_Learn_Input(LayN, nPEs, Datap)
  484. NINT  LayN;
  485. NINT  nPEs;
  486. SREAL *Datap;
  487. {
  488.    NINT wx, wn;
  489.    NINT ret_val;
  490.    
  491.    if ( (ret_val=IsTSOk()) == UIO_OK) {
  492.       
  493.       if ( nPEs > InputLen )
  494.          InputLen = nPEs;
  495.       
  496.       /* pick next training input randomly */
  497.       wn = rand() % (TSetL - nPEs - 1);
  498.       
  499.       for (wx = 0; wx < nPEs; wx++, wn++)
  500.          Datap[wx] = TrainSet[wn];
  501.       
  502.       /*desired response*/
  503.       LastDR = TrainSet[wn];
  504.    }      
  505.    return(ret_val);
  506. }
  507.  
  508. NINT UIO_Learn_Output(LayN, nPEs, Datap)
  509. NINT  LayN;
  510. NINT  nPEs;
  511. SREAL *Datap;
  512. {
  513.    Datap[0] = LastDR;
  514.    return(UIO_OK);
  515. }
  516.  
  517.  
  518. NINT UIO_Learn_Result(LayN, nPEs, Datap)
  519. NINT  LayN;
  520. NINT  nPEs;
  521. SREAL *Datap;
  522. {
  523.    NINT ret_val = UIO_OK;
  524.    
  525.    return(ret_val);
  526. }
  527.  
  528.  
  529. NINT UIO_Learn_End()
  530. {
  531.    NINT ret_val = UIO_OK;
  532.    
  533.    return(ret_val);
  534. }
  535.  
  536.  
  537. NINT UIO_Recall_Start()
  538. {
  539.    NINT ret_val;
  540.  
  541.    /* open all windows at this time */
  542.    SetParms();
  543.       
  544.    ug_window( W0, 0, 80, 3, 80+W0_Wid, 3+W0_Ht );
  545.       
  546.    PlotSet( &MWB, W0, (char *)null_ptr, EX0,EY0,EX1,EY1,
  547.            0, (NINT)(3.4*(EX1-EX0)/NTPTS), EHT / 2.1, 0.0,
  548.            (SREAL *)null_ptr, (TEXT *)null_ptr, 0 );
  549.  
  550.    if ( TSetL <= 0 )
  551.       ReadTS( &buf[0] ); /* read data */
  552.    
  553.    if ((ret_val=IsTSOk()) != UIO_OK)
  554.       return(ret_val);
  555.       
  556.    DataCtr  = 0;
  557.    maxe     = 0.0;
  558.    maxect   = 0;
  559.    maxe2    = 0.0;
  560.    maxe2ct  = 0;
  561.    TotErr   = 0.0;
  562.       
  563.    ug_winclr( 0 );
  564.       
  565.    xlag     = 0;
  566.    HitRate  = 0;
  567.    HitTen   = 0;
  568.    TotalCt  = 0;
  569.       
  570.    return(ret_val);
  571. }
  572.  
  573. NINT UIO_Read(LayN, nPEs, Datap)
  574. NINT  LayN;
  575. NINT  nPEs;
  576. SREAL *Datap;
  577. {
  578.    NINT wx, wn;
  579.    NINT ret_val;
  580.       
  581.    if ((ret_val=IsTSOk()) == UIO_OK) {
  582.    
  583.       if ( nPEs > InputLen )
  584.          InputLen = nPEs;
  585.    
  586.       /* select the next string from the data set */
  587.       PrevTen = CurTen;
  588.       CurTen = 0.0;
  589.       for (wn = DataCtr, wx = 0; wx < nPEs; wx++, wn++ ) {
  590.          CurTen     += TrainSet[wn];
  591.          Datap[wx]   = TrainSet[wn];
  592.       }
  593.       
  594.       TenPred[wn] =
  595.          (1.0 * CurTen) / InputLen;  /* save moving average prediction */
  596.       
  597.       PrevWk = LastDR;    /* save previous week */
  598.       LastDR = TrainSet[wn];
  599.    }
  600.    return(ret_val);
  601. }
  602.  
  603. NINT UIO_Write(LayN, nPEs, Datap)
  604. NINT  LayN;
  605. NINT  nPEs;
  606. SREAL *Datap;
  607. {
  608.    SREAL  v;
  609.    NINT   ret_val = UIO_OK;
  610.  
  611.    NetPred[DataCtr+InputLen] = Datap[0];  /* current prediction */
  612.    
  613.    if ( xlag == 0 ) {
  614.       xlag = InputLen;
  615.       MWB.XOff = xlag;
  616.    }
  617.    DataCtr++;
  618.       
  619.    /* see if it correctly predicted up/down trend */
  620.    if ( fabs(Datap[0]) > .001 )
  621.       v = fabs((Datap[0]-LastDR)/Datap[0]);
  622.    else
  623.       v = 0.0;
  624.  
  625.    if ( v > .10 ) {
  626.       if ( v > maxe2 )
  627.          maxe2 = v;
  628.       maxe2ct++;
  629.    } else {
  630.       if ( v > maxe )
  631.          maxe = v;
  632.       maxect++;
  633.    }
  634.    TotErr += v;
  635.    
  636.    if ( DataCtr > 1 ) {
  637.       SREAL act, prd, ten;
  638.  
  639.       act = LastDR    - PrevWk;
  640.       prd = Datap[0]  - PrevPrd;
  641.       ten = CurTen    - PrevTen;
  642.          
  643.       /* classify network hit rate */
  644.       if ( (fabs(act) < .002 && fabs(prd) < .002) ||
  645.           (act < 0.0 && prd < 0.0) || (act > 0.0 && prd > 0.0) )
  646.          HitRate++;
  647.       if ( (fabs(act) < .002 && fabs(ten) < .002) ||
  648.           (act < 0.0 && ten < 0.0) || (act > 0.0 && ten > 0.0) )
  649.          HitTen++;
  650.       TotalCt++;
  651.    }
  652.    PrevPrd = Datap[0];
  653.    PrevWk  = LastDR;
  654.       
  655.    if(DataCtr >= (TSetL - InputLen-1)) {
  656.       ret_val = UIO_ERROR;
  657.    }
  658.    return(ret_val);
  659. }
  660.  
  661. NINT UIO_Write_Step(LayN, nPEs, Datap)
  662. NINT  LayN;
  663. NINT  nPEs;
  664. SREAL *Datap;
  665. {
  666.    NINT ret_val = UIO_OK;
  667.    
  668.    return(ret_val);
  669. }
  670.  
  671. NINT UIO_Recall_Test(LayN, nPEs, Datap)
  672. NINT  LayN;
  673. NINT  nPEs;
  674. SREAL *Datap;
  675. {
  676.    Datap[0] = LastDR;
  677.    return(UIO_OK);
  678. }
  679.  
  680. NINT UIO_Recall_End()
  681. {
  682.    NINT ret_val = UIO_OK;
  683.    
  684.    if ( TotalCt == 0 )
  685.       TotalCt = 1;
  686.       
  687.    MWB.XOff = 0;
  688.    PlotClear( &MWB );
  689.    PlotArray( &MWB, &TrainSet[0], (SREAL *)null_ptr, TSetL, 0 );
  690.    MWB.XOff = InputLen;
  691.    PlotArray( &MWB, &NetPred[InputLen], &TrainSet[InputLen],
  692.              TSetL-InputLen-1, W0_Sep*1 );
  693.    PlotArray( &MWB, &TenPred[InputLen], &TrainSet[InputLen],
  694.              TSetL-InputLen-1, W0_Sep*2 );
  695.       
  696.    /* put up titles */
  697.    sprintf( buf, "%ld-Week Moving Average", (SL) InputLen );
  698.    ug_puts( W0, 13, 0, MX_ALL+5, MY_1, "S&P 500", 0 );
  699.    ug_puts( W0, 13, 0, MX_ALL+5, MY_2, "Network Prediction", 0 );
  700.    ug_puts( W0, 13, 0, MX_ALL+5, MY_3, buf, 0 );
  701.       
  702.    DataCtr = 0;
  703.    sprintf( buf,
  704.            "Trends:  Net: %ld/%ld (%ld%%), Ave Err=%.3f%%; %ld-Week: %ld/%ld (%ld%%)",
  705.            (SL)HitRate, (SL)TotalCt,
  706.            (SL) ((HitRate * 100 + TotalCt/2) / TotalCt),
  707.            TotErr / TotalCt * 100.,
  708.            (SL) InputLen,
  709.            (SL) HitTen,  (SL) TotalCt,
  710.            (SL) ((HitTen  * 100. + TotalCt/2) / TotalCt ));
  711.       
  712.    PutStr( buf );
  713.    
  714.    GetStr();
  715.  
  716.    return(ret_val);
  717. }
  718.  
  719. NINT UIO_Instrument(Instrument_id, nDataElems, DataElemp)
  720. NINT  Instrument_id;
  721. NINT  nDataElems;
  722. SREAL *DataElemp;
  723. {
  724.    NINT ret_val = UIO_OK;
  725.    
  726.    return(ret_val);
  727. }
  728.  
  729. NINT UIO_ObjFunc(eoeflag, DataElemp)
  730. NINT  eoeflag;
  731. SREAL *DataElemp;
  732. {
  733.    NINT ret_val = UIO_OK;
  734.    
  735.    return(ret_val);
  736. }
  737.  
  738. NINT UIO_Rewind()
  739. {
  740.    NINT ret_val = UIO_OK;
  741.    
  742.    return(ret_val);
  743. }
  744.  
  745. NINT UIO_Explain(LayN, nPEs, Datap)
  746. NINT  LayN;
  747. NINT  nPEs;
  748. SREAL *Datap;
  749. {
  750.    NINT ret_val = UIO_OK;
  751.    
  752.    return(ret_val);
  753. }
  754.